---
title: "Balancing algorithm report"
output:
  html_document:
    df_print: paged
editor_options:
  chunk_output_type: console
---

```{r,message=FALSE,echo=FALSE}
library(dplyr)
```

```{r,echo=FALSE,message=FALSE,warning=FALSE}
df <- read.csv("slovakia_clean.csv")

est <- read.csv("Slovakiaestimates.csv")
est$est=est$est/100
est$Freq=nrow(df) * est$est
est$est=NULL
X <- split(est, est$variable)
X=lapply(X, function(x) { x["variable"] <- NULL; x })
list2env(X ,.GlobalEnv)
joint.d=rename(joint, joint=label)
pol.d=rename(pol, pol=label)
sex.d=rename(sex, sex=label)
vot.d=rename(vot, vot=label)

library(survey)
df$ids=seq(1:nrow(df))
df1 <- svydesign(ids=~1, data=df)

df.r <- rake(design = df1,
             sample.margins = list(~joint, ~pol, ~sex, ~vot),
             population.margins = list(joint.d, pol.d, sex.d, vot.d))

df.r1 <- trimWeights(df.r, lower=0.6, upper=5,
                                   strict=TRUE) 
df$w=weights(df.r1)
df$Freq=round(df$w,0)

df0=select(df,Record.ID,joint,pol,sex,vot,Freq)
MyData <- df0[rep(row.names(df0), df0$Freq), 1:5]
write.csv(MyData,"MyData.csv",row.names=F)

```

# Python (config = code) 
```{python engine.path="/usr/bin/python3.5",results='hide'}
import sys
sys.path.append('/home/shared/data/balancing/')
import drop
config = "{1: {'degreeHigh':9.8,'noDegreeHigh':38.8,'degreeLow':12.4,'noDegreeLow':38.9}},{2: {'interest':57.9,'LowInterest':42.1}},{3: {'Male':47.3,'Female':52.7}},{4: {'SMER':26.82,'KDH':14.72,'SDKU':8.63,'OLaNO':8.31,'NOVA':7.61,'SaS':7.42,'Most':6.49,'Undec':20.00}}"

filename = '/home/fernando/Misc/bjpolFinal/Slovakia/MyData.csv'

drop.run(config, filename, "/home/fernando/Misc/bjpolFinal/Slovakia/myBal", 2.5)
```

